<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<script type="text/javascript">
	var userDataGrid = $('#user-datagrid-json').datagrid({
	    gridTitle : '用户管理',
	    showToolbar: true,
	    toolbarItem: '',
	    addLocation: 'first',
	    showLinenumber: false,
	    filterThead: false,
	    filterAll: true,
	    sortAll : true,
	    dataUrl: 'user/list.do',
	    dataType: 'json',
	    columns: [
	    	{
				name: 'status',
				label: '操作',
				align: 'center',
				width: 280,
				quicksort:false,
				menu: false,
				edit: false,
				render: function(value){
			        var html = '';
			    	html += '<div class="btn-group" role="group"><button type="button" class="btn btn-orange btn-sm" onclick="setPwd(this);"><i class="fa fa-edit"></i> 重置密码</button></div>';
	
				    if(value == 0){
						html += '<div class="btn-group" role="group"><button type="button" class="btn btn-green btn-sm" onclick="changeStatus(this,1)"><i class="fa fa-edit"></i> 解锁</button></div>';
					}else{
						html += '<div class="btn-group" role="group"><button type="button" class="btn btn-orange btn-sm" onclick="changeStatus(this,0)"><i class="fa fa-edit"></i> 锁定</button></div>';
					}
			    	html += '<div class="btn-group" role="group"><button type="button" class="btn btn-blue btn-sm" onclick="editUser(this)"><i class="fa fa-edit"></i> 编辑</button></div>';
				    html += '<div class="btn-group" role="group"><button type="button" class="btn btn-red btn-sm" onclick="delUser(this)"><i class="fa fa-remove"></i> 删除</button></div>';
				    return html;
				}
	        },
	        {
	            name: 'loginName',
	            label: '登录账号',
	            align: 'center',
	            /*filterDatas:{
		            operatorA : 'like',
		            valA      : 'admin',
		            andor     : 'or',
		            operatorB : 'like',
		            valB      : 'test'
		        },*/
	            width: 100
	        },
	        {
	            name: 'userName',
	            label: '用户名称',
	            align: 'center',
	            width: 100
	        },
	        {
	            name: 'status',
	            label: '状态',
	            type : 'select',
	            items: [{'0':'锁定'}, {'1':'正常'}],
	            align: 'center',
	            width: 60,
	            render: $.datagrid.renderItem
	        },
	        {
	            name: 'changePass',
	            label: '已修改密码',
	            type : 'select',
	            items: [{'0':'已修改'}, {'1':'未修改'}],
	            align: 'center',
	            width: 120,
	            render: $.datagrid.renderItem
	        },
	        {
	            name: 'lastLoginTime',
	            label: '最近一次登录时间',
	            type : 'date',
	            pattern: 'yyyy-MM-dd HH:mm:ss',
	            align: 'center',
	            width: 180,
	            render: function(value) {
					return value;
		        }
	        }
	    ],
	    afterSave: function($trs, datas) {
	        	this.refresh();
	        },
	    paging: {pageSize:20, selectPageSize:'20,30,40'},
	    showTfoot: true,
	    fullGrid: false,
	    //initLoadData:false,
	    toolbarCustom: function(){
	    	var html = '';
	    	html += '<div class="btn-group" role="group"><button type="button" class="btn btn-blue" onclick="addUser()"><i class="fa fa-plus"></i> 添加</button></div>';
		    html += '<div class="btn-group" role="group"><button type="button" class="btn btn-green" onclick="refreshUser()"><i class="fa fa-refresh"></i> 刷新</button></div>';
		    return html;
	    }
	});

	try {
		userDataGrid.data('bjui.datagrid').colLock(0, true);
	} catch (e) {}
	
	function changeStatus(_btn, status){
		var trIndex = $(_btn).closest("tr").index();
		var rowData = userDataGrid.data('bjui.datagrid').getRowData(trIndex);

		if(rowData.name == "admin"){
			userDataGrid.alertmsg('error', "不允许修改超级管理员的状态");
			return;
		}

		var url = "user/changeStatus.do?status="+status+"&userSn="+rowData.userSn;
		var data = null;
		var successCallBack = function(result){
			refreshUser();
		}
		var failCallBack = null;
		var errorCallBack = null;
		myAjax(url, data, true, successCallBack, failCallBack, errorCallBack);
	}
	
	function refreshUser() {
		userDataGrid.datagrid("refresh");
	}

	function setPwd(_btn){
		var trIndex = $(_btn).closest("tr").index();
		var res = userDataGrid.data('bjui.datagrid').getRowData(trIndex);

		//超级管理员不允许修改密码
		if(res.name == "admin"){
			userDataGrid.alertmsg('error', "不允许修改超级管理员的密码");
			return;
		}
		
		$('#edit-user-dialog').dialog({
			id:'user-password-dialog', 
			url:'user/password.do?userSn='+res.userSn, 
			maxable: false,
			mask: true,
			width: 600,
			height: 260,
			onLoad: function($dialog){
				setPasswordBtnEvent($dialog);
			},
			title:'重置密码'
		});
	}

	function setPasswordBtnEvent($dialog, isAdd) {
		var $save, $cancel;
		$save = $dialog.find('.save');
		$cancel = $dialog.find('.cancel');
		$save.click(function(){
			$('#user_password_form').isValid(function(v) {
				if(v){
					$dialog.alertmsg('confirm', '是否确认重置原密码', {
						displayMode:'slide',
						okName:'确定', 
		                cancelName:'取消', 
		                title:'提示信息',
		                okCall : function(){
		                	var url = 'user/setPassword.do';
		            		var data = $('#user_password_form').serialize();
		            		var successCallBack = function(result){
		            			$dialog.dialog('closeCurrent');
								userDataGrid.datagrid('refresh');
		            		}
		            		var failCallBack = null;
		            		var errorCallBack = null;
		            		myAjax(url, data, true, successCallBack, failCallBack, errorCallBack);
		                }
					});
				}
			});
		});
	
		$cancel.click(function(){
			$dialog.dialog('closeCurrent');
		});
	}

	var userIndex=-1;
	function addUser() {
		userIndex = -1;
		$('#edit-user-dialog').dialog({
			id:'edit-user-dialog', 
			url:'user/add.do', 
			maxable: false,
			mask: true,
			width: 850,
			height: 500,
			onLoad: function($dialog){
				setEditUserBtnEvent($dialog, true);
			},
			title:'用户管理 - 添加'});
	}
	function editUser(_this) {
		userIndex = $(_this).closest("tr").index();
		var data = userDataGrid.data('bjui.datagrid').getRowData(userIndex);
		$('#edit-user-dialog').dialog({
			id:'edit-user-dialog',
			url:'user/edit.do?userNo='+data.userNo,
			maxable: false,
			mask: true,
			width: 850,
			height: 500,
			onLoad: function($dialog){
				
				setEditUserBtnEvent($dialog, false);
				
				// 根据data中的loginName，取得数据并加载到form表单中
				getJsonData2Form('user/getUser.do', 'userSn='+data.userSn, $('#user_form'), userCallback);
				
			},
			
			title:'用户管理 - 编辑'});
	}
	function delUser(_btn){
		userIndex = $(_btn).closest("tr").index();
		var rowData = userDataGrid.data('bjui.datagrid').getRowData(userIndex);

		userDataGrid.alertmsg('confirm', '确定删除当前数据？', 
			{
			    displayMode:'slide', 
	            okName:'确定', 
	            cancelName:'取消', 
	            title:'提示信息',
	            okCall : function(){
	            	var url = "user/del.do?userSn="+rowData.userSn;
            		var data = null;
            		var successCallBack = function(result){
            			refreshUser();
            		}
            		var failCallBack = null;
            		var errorCallBack = null;
            		myAjax(url, data, true, successCallBack, failCallBack, errorCallBack);
	            }
	    });
	}
	 function userCallback($inputObj, _name, _val){
		//$("#l_userNo").attr("readonly","readonly");
	}
	
	function setEditUserBtnEvent($dialog, isAdd) {
		var $prev, $next, $save, $cancel;
		$save = $dialog.find('.save');
		$cancel = $dialog.find('.cancel');
	
		$save.click(function(){
			$('#user_form').isValid(function(v) {
				if(v){
					var url = 'user/save.do';
            		var data = $('#user_form').serialize()+"&addFlag="+isAdd;
            		var successCallBack = function(result){
            			$dialog.dialog('closeCurrent');
            			refreshUser();
            		}
            		var failCallBack = null;
            		var errorCallBack = null;
            		myAjax(url, data, true, successCallBack, failCallBack, errorCallBack);
				}
			});
		});
	
		$cancel.click(function(){
			$dialog.dialog('closeCurrent');
		});
	}
</script>
<div class="bjui-pageContent" id="user-index-pagecontent">
    <div style="padding:0px; height:100%; width:100%;">
        <table id="user-datagrid-json" data-width="100%" data-height="100%" class="table table-bordered">
        </table>
    </div>
</div>
<div id="edit-user-dialog" style="display: none;">
</div>
<div id="set-password-dialog" style="display: none;">
</div>